//
//  YTGridView.m
//  YUNT
//
//  Created by 梁炜东 on 2017/11/18.
//  Copyright © 2017年 梁炜东. All rights reserved.
//

#import "YTGridView.h"

@interface YTGridView()
@property(nonatomic, strong)UIView *horiLine1;
@property(nonatomic, strong)UIView *horiLine2;
@property(nonatomic, strong)UIView *verLine1;
@property(nonatomic, strong)UIView *verLine2;
//@property(nonatomic, strong)UIView *biasLine1;
//@property(nonatomic, strong)UIView *biasLine2;
@property(nonatomic, strong)CAShapeLayer *shapeLayer1;
@property(nonatomic, strong)CAShapeLayer *shapeLayer2;
@property(nonatomic, assign)CGMutablePathRef pathRef1;
@property(nonatomic, assign)CGMutablePathRef pathRef2;
@property(nonatomic, strong)UIImageView *centerImage;
@end

@implementation YTGridView
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.userInteractionEnabled = YES;
        [self p_addSubViews];
        [self p_makeConstraints];
        [self configGrid];
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    [self p_makeConstraints];
}
-(void)configGrid
{
    switch (publicModel.gridModel)
    {
        case 0:
        {
            [self removeFromSuperview];
        }
            break;
        case 1:
        {
            _horiLine1.hidden = NO;
            _horiLine2.hidden = NO;
            _verLine1.hidden = NO;
            _verLine2.hidden = NO;
            _shapeLayer1.hidden = YES;
            _shapeLayer2.hidden = YES;
            _centerImage.hidden = YES;
            
        }
            break;
        case 2:
        {
            _horiLine1.hidden = NO;
            _horiLine2.hidden = NO;
            _verLine1.hidden = NO;
            _verLine2.hidden = NO;
            _shapeLayer1.hidden = NO;
            _shapeLayer2.hidden = NO;
            _centerImage.hidden = YES;
        }
            break;
        case 3:
        {
            _horiLine1.hidden = YES;
            _horiLine2.hidden = YES;
            _verLine1.hidden = YES;
            _verLine2.hidden = YES;
            _shapeLayer1.hidden = YES;
            _shapeLayer2.hidden = YES;
            _centerImage.hidden = NO;
        }
            break;
        default:
            break;
    }
}
-(void)p_addSubViews
{
    _horiLine1 = [[UIView alloc] init];
    _horiLine1.backgroundColor = [UIColor whiteColor];
    _horiLine1.hidden = YES;
    [self addSubview:_horiLine1];
    
    _horiLine2 = [[UIView alloc] init];
    _horiLine2.backgroundColor = [UIColor whiteColor];
    _horiLine2.hidden = YES;
    [self addSubview:_horiLine2];
    
    _verLine1 = [[UIView alloc] init];
    _verLine1.backgroundColor = [UIColor whiteColor];
    _verLine1.hidden = YES;
    [self addSubview:_verLine1];
    
    _verLine2 = [[UIView alloc] init];
    _verLine2.backgroundColor = [UIColor whiteColor];
    _verLine2.hidden = YES;
    [self addSubview:_verLine2];
    
    _centerImage = [[UIImageView alloc] init];
    _centerImage.image = IMAGE(@"49");
    [self addSubview:_centerImage];
    
    [self p_layer];
}
-(void)p_layer
{
    if([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortrait||[UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortraitUpsideDown)
    {//竖屏
        [_shapeLayer1 removeFromSuperlayer];
        [_shapeLayer2 removeFromSuperlayer];
        _pathRef1 = CGPathCreateMutable();
        CGPathMoveToPoint(_pathRef1, NULL, 0, 0);
        CGPathAddLineToPoint(_pathRef1, NULL, ScreenWidth, ScreenHeight);
        _shapeLayer1 = [CAShapeLayer layer];
        _shapeLayer1.strokeColor = [UIColor whiteColor].CGColor;
        _shapeLayer1.fillColor = [UIColor clearColor].CGColor;
        _shapeLayer1.lineWidth = 1.f;
        _shapeLayer1.lineCap = kCALineCapButt;
        _shapeLayer1.path = _pathRef1;
        _shapeLayer2.hidden = YES;
        [self.layer addSublayer:_shapeLayer1];
        
        _pathRef2 = CGPathCreateMutable();
        CGPathMoveToPoint(_pathRef2, NULL, ScreenWidth, 0);
        CGPathAddLineToPoint(_pathRef2, NULL, 0, ScreenHeight);
        _shapeLayer2 = [CAShapeLayer layer];
        _shapeLayer2.strokeColor = [UIColor whiteColor].CGColor;
        _shapeLayer2.fillColor = [UIColor clearColor].CGColor;
        _shapeLayer2.lineWidth = 1.f;
        _shapeLayer2.lineCap = kCALineCapButt;
        _shapeLayer2.path = _pathRef2;
        _shapeLayer2.hidden = YES;
        [self.layer addSublayer:_shapeLayer2];
    }else
    {
        [_shapeLayer1 removeFromSuperlayer];
        [_shapeLayer2 removeFromSuperlayer];
        _pathRef1 = CGPathCreateMutable();
        CGPathMoveToPoint(_pathRef1, NULL, 0, ScreenHeight);
        CGPathAddLineToPoint(_pathRef1, NULL, ScreenWidth, 0);
        _shapeLayer1 = [CAShapeLayer layer];
        _shapeLayer1.strokeColor = [UIColor whiteColor].CGColor;
        _shapeLayer1.fillColor = [UIColor clearColor].CGColor;
        _shapeLayer1.lineWidth = 1.f;
        _shapeLayer1.lineCap = kCALineCapButt;
        _shapeLayer1.path = _pathRef1;
        _shapeLayer2.hidden = YES;
        [self.layer addSublayer:_shapeLayer1];
        
        _pathRef2 = CGPathCreateMutable();
        CGPathMoveToPoint(_pathRef2, NULL, 0, 0);
        CGPathAddLineToPoint(_pathRef2, NULL, ScreenWidth, ScreenHeight);
        _shapeLayer2 = [CAShapeLayer layer];
        _shapeLayer2.strokeColor = [UIColor whiteColor].CGColor;
        _shapeLayer2.fillColor = [UIColor clearColor].CGColor;
        _shapeLayer2.lineWidth = 1.f;
        _shapeLayer2.lineCap = kCALineCapButt;
        _shapeLayer2.path = _pathRef2;
        _shapeLayer2.hidden = YES;
        [self.layer addSublayer:_shapeLayer2];
    }
    [self configGrid];
}
-(void)p_makeConstraints
{
    if (publicModel.gridModel == 2)
    {
        [self p_layer];
    }
    [_horiLine1 mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self);
        make.top.equalTo(self).offset(ScreenHeight/3 - 0.5);
        make.height.mas_equalTo(1);
    }];
    [_horiLine2 mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self);
        make.top.equalTo(_horiLine1).offset(ScreenHeight/3 - 0.5);
        make.height.mas_equalTo(1);
    }];
    [_verLine1 mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.leading.equalTo(self).offset(ScreenWidth/3 - 0.5);
        make.width.mas_equalTo(1);
    }];
    [_verLine2 mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.leading.equalTo(_verLine1).offset(ScreenWidth/3 - 0.5);
        make.width.mas_equalTo(1);
    }];
    [_centerImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.mas_equalTo(58/2);
        make.height.mas_equalTo(40/2);
    }];

}
@end
